package everydayGrade.t1;

import org.junit.jupiter.api.Test;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.PriorityQueue;

/**
 * @Author: Siantar
 * @Date: 2023-07-21-10:08
 * @Description: 1.0
 */
public class T1499 {
    public int findMaxValueOfEquation(int[][] points, int k) {
        int res = Integer.MIN_VALUE;
        Deque<int[]> q = new ArrayDeque<>();
        for (int[] p : points) {
            int x = p[0], y = p[1];
            while (!q.isEmpty() && q.peekFirst()[0] < x - k) {
                q.pollFirst();
            }
            if (!q.isEmpty()) {
                res = Math.max(res, q.peekFirst()[1] + x + y);
            }
            while (!q.isEmpty() && q.peekLast()[1] <= y - x) {
                q.pollLast();
            }
            q.offerLast(new int[]{x, y - x});
        }
        return res;
    }

    //    points = [[1,3],[2,0],[5,10],[6,-10]], k = 1
    @Test
    public void test() {


        System.out.println(findMaxValueOfEquation(new int[][]{{1, 3}, {2, 0}, {5, 10}, {6, -10}}, 1));


//        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
//        pq.offer(2);
//        pq.offer(1);
//        pq.offer(3);
//        pq.offer(3);
//        System.out.println(pq.poll());
//        System.out.println(pq.poll());
//        System.out.println(pq.poll());
    }
}
